package com.example.psysystemhomework;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import com.example.psysystemhomework.util.AppManager;
import com.example.psysystemhomework.util.NetUtil;
import com.example.psysystemhomework.util.ShareUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.psysystemhomework.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    ShareUtil shareUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         shareUtil = new ShareUtil(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.log_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.exit_login:
                AppManager appManager = AppManager.getAppManager();
                appManager.addActivity(new LoginActivity());
                Intent intent = new Intent(this, appManager.currentActivity().getClass());
shareUtil.saveUser(null);
                startActivity(intent);
                break;
            case R.id.host_update:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("修改主机名称");
                EditText view = new EditText(this);
                view.setText(shareUtil.getHost());
                builder.setView(view);
                builder.setPositiveButton("确定修改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Editable text = view.getText();
                        shareUtil.saveHost(text.toString());
                        Toast.makeText(getApplicationContext(), "修改成功" + text.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
                builder.show();

            default:
                break;

        }


        return super.onOptionsItemSelected(item);
    }
}